package com.zlove.common.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;

public class SyncHorizontalScrollView extends HorizontalScrollView {

	private View view;
	private ImageView leftImage;
	private ImageView rightImage;
	private int windowWitdh = 0;
	private Activity mContext;

	/***
	 * 设置 标签布局
	 * 
	 * @param view
	 *            布局文件rl_tab指示的view
	 * @param leftImage
	 *            左指示标
	 * @param rightImage
	 *            右指示标
	 * @param context
	 *            使用结构如下 <RelativeLayout android:id="@+id/rl_tab"
	 *            android:layout_width="fill_parent"
	 *            android:layout_height="wrap_content"
	 *            android:background="#F2F2F2" >
	 * 
	 *            <com.example.playtabtest.view.SyncHorizontalScrollView
	 *            android:id="@+id/mHsv" android:layout_width="fill_parent"
	 *            android:layout_height="40dip" android:fadingEdge="none"
	 *            android:scrollbars="none" >
	 * 
	 *            <RelativeLayout android:id="@+id/rl_nav"
	 *            android:layout_width="fill_parent"
	 *            android:layout_height="wrap_content"
	 *            android:layout_gravity="top" android:background="#5AB0EB" >
	 * 
	 *            <RadioGroup android:id="@+id/rg_nav_content"
	 *            android:layout_width="fill_parent"
	 *            android:layout_height="38dip"
	 *            android:layout_alignParentTop="true"
	 *            android:background="#F2F2F2" android:orientation="horizontal"
	 *            > </RadioGroup>
	 * 
	 *            <ImageView android:id="@+id/iv_nav_indicator"
	 *            android:layout_width="1dip" android:layout_height="5dip"
	 *            android:layout_alignParentBottom="true"
	 *            android:background="#5AB0EB"
	 *            android:contentDescription="@string/nav_desc"
	 *            android:scaleType="matrix" /> </RelativeLayout>
	 *            </com.example.playtabtest.view.SyncHorizontalScrollView>
	 *            //左指示标 <ImageView android:id="@+id/iv_nav_left"
	 *            android:layout_width="wrap_content"
	 *            android:layout_height="wrap_content"
	 *            android:layout_alignParentLeft="true"
	 *            android:layout_centerVertical="true"
	 *            android:contentDescription="@string/nav_desc"
	 *            android:paddingBottom="1dip"
	 *            android:src="@drawable/iv_navagation_scroll_left"
	 *            android:visibility="gone" > </ImageView> //右指示标 <ImageView
	 *            android:id="@+id/iv_nav_right"
	 *            android:layout_width="wrap_content"
	 *            android:layout_height="wrap_content"
	 *            android:layout_alignParentRight="true"
	 *            android:layout_centerVertical="true"
	 *            android:contentDescription="@string/nav_desc"
	 *            android:paddingBottom="1dip"
	 *            android:src="@drawable/iv_navagation_scroll_right"
	 *            android:visibility="visible" > </ImageView> </RelativeLayout>
	 */
	public void setSomeParam(View view, ImageView leftImage,
			ImageView rightImage, Activity context) {
		this.mContext = context;
		this.view = view;
		this.leftImage = leftImage;
		this.rightImage = rightImage;
		DisplayMetrics dm = new DisplayMetrics();
		this.mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
		windowWitdh = dm.widthPixels;
	}

	public SyncHorizontalScrollView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public SyncHorizontalScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	// 显示和隐藏左右两边的箭头
	public void showAndHideArrow() {
		if (!mContext.isFinishing() && view != null) {
			this.measure(0, 0);
			if (windowWitdh >= this.getMeasuredWidth()) {
				leftImage.setVisibility(View.GONE);
				rightImage.setVisibility(View.GONE);
			} else {
				if (this.getLeft() == 0) {
					leftImage.setVisibility(View.GONE);
					rightImage.setVisibility(View.VISIBLE);
				} else if (this.getRight() == this.getMeasuredWidth()
						- windowWitdh) {
					leftImage.setVisibility(View.VISIBLE);
					rightImage.setVisibility(View.GONE);
				} else {
					leftImage.setVisibility(View.VISIBLE);
					rightImage.setVisibility(View.VISIBLE);
				}
			}
		}
	}

	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		if (!mContext.isFinishing() && view != null && rightImage != null
				&& leftImage != null) {
			if (view.getWidth() <= windowWitdh) {
				leftImage.setVisibility(View.GONE);
				rightImage.setVisibility(View.GONE);
			} else {
				if (l == 0) {
					leftImage.setVisibility(View.GONE);
					rightImage.setVisibility(View.VISIBLE);
				} else if (view.getWidth() - l == windowWitdh) {
					leftImage.setVisibility(View.VISIBLE);
					rightImage.setVisibility(View.GONE);
				} else {
					leftImage.setVisibility(View.VISIBLE);
					rightImage.setVisibility(View.VISIBLE);
				}
			}
		}
	}
}
